home *** CD-ROM | disk | FTP | other *** search
/ PC/CD Gamer UK 120 / CD Gamer Issue 120 (March 2003) (Disc 2).ISO / mods / Q2_Codered / codeRED1_0.exe / Data1.cab / g_func.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-13  |  51.9 KB  |  2,048 lines

  1. /*
  2. Copyright (C) 1997-2001 Id Software, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  12.  
  13. See the GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. */
  20. #include "g_local.h"
  21.  
  22. /*
  23. =========================================================
  24.  
  25.   PLATS
  26.  
  27.   movement options:
  28.  
  29.   linear
  30.   smooth start, hard stop
  31.   smooth start, smooth stop
  32.  
  33.   start
  34.   end
  35.   acceleration
  36.   speed
  37.   deceleration
  38.   begin sound
  39.   end sound
  40.   target fired when reaching end
  41.   wait at end
  42.  
  43.   object characteristics that use move segments
  44.   ---------------------------------------------
  45.   movetype_push, or movetype_stop
  46.   action when touched
  47.   action when blocked
  48.   action when used
  49.     disabled?
  50.   auto trigger spawning
  51.  
  52.  
  53. =========================================================
  54. */
  55.  
  56. #define PLAT_LOW_TRIGGER    1
  57.  
  58. #define    STATE_TOP            0
  59. #define    STATE_BOTTOM        1
  60. #define STATE_UP            2
  61. #define STATE_DOWN            3
  62.  
  63. #define DOOR_START_OPEN        1
  64. #define DOOR_REVERSE        2
  65. #define DOOR_CRUSHER        4
  66. #define DOOR_NOMONSTER        8
  67. #define DOOR_TOGGLE            32
  68. #define DOOR_X_AXIS            64
  69. #define DOOR_Y_AXIS            128
  70.  
  71.  
  72. //
  73. // Support routines for movement (changes in origin using velocity)
  74. //
  75.  
  76. void Move_Done (edict_t *ent)
  77. {
  78.     VectorClear (ent->velocity);
  79.     ent->moveinfo.endfunc (ent);
  80. }
  81.  
  82. void Move_Final (edict_t *ent)
  83. {
  84.     if (ent->moveinfo.remaining_distance == 0)
  85.     {
  86.         Move_Done (ent);
  87.         return;
  88.     }
  89.  
  90.     VectorScale (ent->moveinfo.dir, ent->moveinfo.remaining_distance / FRAMETIME, ent->velocity);
  91.  
  92.     ent->think = Move_Done;
  93.     ent->nextthink = level.time + FRAMETIME;
  94. }
  95.  
  96. void Move_Begin (edict_t *ent)
  97. {
  98.     float    frames;
  99.  
  100.     if ((ent->moveinfo.speed * FRAMETIME) >= ent->moveinfo.remaining_distance)
  101.     {
  102.         Move_Final (ent);
  103.         return;
  104.     }
  105.     VectorScale (ent->moveinfo.dir, ent->moveinfo.speed, ent->velocity);
  106.     frames = floor((ent->moveinfo.remaining_distance / ent->moveinfo.speed) / FRAMETIME);
  107.     ent->moveinfo.remaining_distance -= frames * ent->moveinfo.speed * FRAMETIME;
  108.     ent->nextthink = level.time + (frames * FRAMETIME);
  109.     ent->think = Move_Final;
  110. }
  111.  
  112. void Think_AccelMove (edict_t *ent);
  113.  
  114. void Move_Calc (edict_t *ent, vec3_t dest, void(*func)(edict_t*))
  115. {
  116.     VectorClear (ent->velocity);
  117.     VectorSubtract (dest, ent->s.origin, ent->moveinfo.dir);
  118.     ent->moveinfo.remaining_distance = VectorNormalize (ent->moveinfo.dir);
  119.     ent->moveinfo.endfunc = func;
  120.  
  121.     if (ent->moveinfo.speed == ent->moveinfo.accel && ent->moveinfo.speed == ent->moveinfo.decel)
  122.     {
  123.         if (level.current_entity == ((ent->flags & FL_TEAMSLAVE) ? ent->teammaster : ent))
  124.         {
  125.             Move_Begin (ent);
  126.         }
  127.         else
  128.         {
  129.             ent->nextthink = level.time + FRAMETIME;
  130.             ent->think = Move_Begin;
  131.         }
  132.     }
  133.     else
  134.     {
  135.         // accelerative
  136.         ent->moveinfo.current_speed = 0;
  137.         ent->think = Think_AccelMove;
  138.         ent->nextthink = level.time + FRAMETIME;
  139.     }
  140. }
  141.  
  142.  
  143. //
  144. // Support routines for angular movement (changes in angle using avelocity)
  145. //
  146.  
  147. void AngleMove_Done (edict_t *ent)
  148. {
  149.     VectorClear (ent->avelocity);
  150.     ent->moveinfo.endfunc (ent);
  151. }
  152.  
  153. void AngleMove_Final (edict_t *ent)
  154. {
  155.     vec3_t    move;
  156.  
  157.     if (ent->moveinfo.state == STATE_UP)
  158.         VectorSubtract (ent->moveinfo.end_angles, ent->s.angles, move);
  159.     else
  160.         VectorSubtract (ent->moveinfo.start_angles, ent->s.angles, move);
  161.  
  162.     if (VectorCompare (move, vec3_origin))
  163.     {
  164.         AngleMove_Done (ent);
  165.         return;
  166.     }
  167.  
  168.     VectorScale (move, 1.0/FRAMETIME, ent->avelocity);
  169.  
  170.     ent->think = AngleMove_Done;
  171.     ent->nextthink = level.time + FRAMETIME;
  172. }
  173.  
  174. void AngleMove_Begin (edict_t *ent)
  175. {
  176.     vec3_t    destdelta;
  177.     float    len;
  178.     float    traveltime;
  179.     float    frames;
  180.  
  181.     // set destdelta to the vector needed to move
  182.     if (ent->moveinfo.state == STATE_UP)
  183.         VectorSubtract (ent->moveinfo.end_angles, ent->s.angles, destdelta);
  184.     else
  185.         VectorSubtract (ent->moveinfo.start_angles, ent->s.angles, destdelta);
  186.     
  187.     // calculate length of vector
  188.     len = VectorLength (destdelta);
  189.     
  190.     // divide by speed to get time to reach dest
  191.     traveltime = len / ent->moveinfo.speed;
  192.  
  193.     if (traveltime < FRAMETIME)
  194.     {
  195.         AngleMove_Final (ent);
  196.         return;
  197.     }
  198.  
  199.     frames = floor(traveltime / FRAMETIME);
  200.  
  201.     // scale the destdelta vector by the time spent traveling to get velocity
  202.     VectorScale (destdelta, 1.0 / traveltime, ent->avelocity);
  203.  
  204.     // set nextthink to trigger a think when dest is reached
  205.     ent->nextthink = level.time + frames * FRAMETIME;
  206.     ent->think = AngleMove_Final;
  207. }
  208.  
  209. void AngleMove_Calc (edict_t *ent, void(*func)(edict_t*))
  210. {
  211.     VectorClear (ent->avelocity);
  212.     ent->moveinfo.endfunc = func;
  213.     if (level.current_entity == ((ent->flags & FL_TEAMSLAVE) ? ent->teammaster : ent))
  214.     {
  215.         AngleMove_Begin (ent);
  216.     }
  217.     else
  218.     {
  219.         ent->nextthink = level.time + FRAMETIME;
  220.         ent->think = AngleMove_Begin;
  221.     }
  222. }
  223.  
  224.  
  225. /*
  226. ==============
  227. Think_AccelMove
  228.  
  229. The team has completed a frame of movement, so
  230. change the speed for the next frame
  231. ==============
  232. */
  233. #define AccelerationDistance(target, rate)    (target * ((target / rate) + 1) / 2)
  234.  
  235. void plat_CalcAcceleratedMove(moveinfo_t *moveinfo)
  236. {
  237.     float    accel_dist;
  238.     float    decel_dist;
  239.  
  240.     moveinfo->move_speed = moveinfo->speed;
  241.  
  242.     if (moveinfo->remaining_distance < moveinfo->accel)
  243.     {
  244.         moveinfo->current_speed = moveinfo->remaining_distance;
  245.         return;
  246.     }
  247.  
  248.     accel_dist = AccelerationDistance (moveinfo->speed, moveinfo->accel);
  249.     decel_dist = AccelerationDistance (moveinfo->speed, moveinfo->decel);
  250.  
  251.     if ((moveinfo->remaining_distance - accel_dist - decel_dist) < 0)
  252.     {
  253.         float    f;
  254.  
  255.         f = (moveinfo->accel + moveinfo->decel) / (moveinfo->accel * moveinfo->decel);
  256.         moveinfo->move_speed = (-2 + sqrt(4 - 4 * f * (-2 * moveinfo->remaining_distance))) / (2 * f);
  257.         decel_dist = AccelerationDistance (moveinfo->move_speed, moveinfo->decel);
  258.     }
  259.  
  260.     moveinfo->decel_distance = decel_dist;
  261. };
  262.  
  263. void plat_Accelerate (moveinfo_t *moveinfo)
  264. {
  265.     // are we decelerating?
  266.     if (moveinfo->remaining_distance <= moveinfo->decel_distance)
  267.     {
  268.         if (moveinfo->remaining_distance < moveinfo->decel_distance)
  269.         {
  270.             if (moveinfo->next_speed)
  271.             {
  272.                 moveinfo->current_speed = moveinfo->next_speed;
  273.                 moveinfo->next_speed = 0;
  274.                 return;
  275.             }
  276.             if (moveinfo->current_speed > moveinfo->decel)
  277.                 moveinfo->current_speed -= moveinfo->decel;
  278.         }
  279.         return;
  280.     }
  281.  
  282.     // are we at full speed and need to start decelerating during this move?
  283.     if (moveinfo->current_speed == moveinfo->move_speed)
  284.         if ((moveinfo->remaining_distance - moveinfo->current_speed) < moveinfo->decel_distance)
  285.         {
  286.             float    p1_distance;
  287.             float    p2_distance;
  288.             float    distance;
  289.  
  290.             p1_distance = moveinfo->remaining_distance - moveinfo->decel_distance;
  291.             p2_distance = moveinfo->move_speed * (1.0 - (p1_distance / moveinfo->move_speed));
  292.             distance = p1_distance + p2_distance;
  293.             moveinfo->current_speed = moveinfo->move_speed;
  294.             moveinfo->next_speed = moveinfo->move_speed - moveinfo->decel * (p2_distance / distance);
  295.             return;
  296.         }
  297.  
  298.     // are we accelerating?
  299.     if (moveinfo->current_speed < moveinfo->speed)
  300.     {
  301.         float    old_speed;
  302.         float    p1_distance;
  303.         float    p1_speed;
  304.         float    p2_distance;
  305.         float    distance;
  306.  
  307.         old_speed = moveinfo->current_speed;
  308.  
  309.         // figure simple acceleration up to move_speed
  310.         moveinfo->current_speed += moveinfo->accel;
  311.         if (moveinfo->current_speed > moveinfo->speed)
  312.             moveinfo->current_speed = moveinfo->speed;
  313.  
  314.         // are we accelerating throughout this entire move?
  315.         if ((moveinfo->remaining_distance - moveinfo->current_speed) >= moveinfo->decel_distance)
  316.             return;
  317.  
  318.         // during this move we will accelrate from current_speed to move_speed
  319.         // and cross over the decel_distance; figure the average speed for the
  320.         // entire move
  321.         p1_distance = moveinfo->remaining_distance - moveinfo->decel_distance;
  322.         p1_speed = (old_speed + moveinfo->move_speed) / 2.0;
  323.         p2_distance = moveinfo->move_speed * (1.0 - (p1_distance / p1_speed));
  324.         distance = p1_distance + p2_distance;
  325.         moveinfo->current_speed = (p1_speed * (p1_distance / distance)) + (moveinfo->move_speed * (p2_distance / distance));
  326.         moveinfo->next_speed = moveinfo->move_speed - moveinfo->decel * (p2_distance / distance);
  327.         return;
  328.     }
  329.  
  330.     // we are at constant velocity (move_speed)
  331.     return;
  332. };
  333.  
  334. void Think_AccelMove (edict_t *ent)
  335. {
  336.     ent->moveinfo.remaining_distance -= ent->moveinfo.current_speed;
  337.  
  338.     if (ent->moveinfo.current_speed == 0)        // starting or blocked
  339.         plat_CalcAcceleratedMove(&ent->moveinfo);
  340.  
  341.     plat_Accelerate (&ent->moveinfo);
  342.  
  343.     // will the entire move complete on next frame?
  344.     if (ent->moveinfo.remaining_distance <= ent->moveinfo.current_speed)
  345.     {
  346.         Move_Final (ent);
  347.         return;
  348.     }
  349.  
  350.     VectorScale (ent->moveinfo.dir, ent->moveinfo.current_speed*10, ent->velocity);
  351.     ent->nextthink = level.time + FRAMETIME;
  352.     ent->think = Think_AccelMove;
  353. }
  354.  
  355.  
  356. void plat_go_down (edict_t *ent);
  357.  
  358. void plat_hit_top (edict_t *ent)
  359. {
  360.     if (!(ent->flags & FL_TEAMSLAVE))
  361.     {
  362.         if (ent->moveinfo.sound_end)
  363.             gi.sound (ent, CHAN_NO_PHS_ADD+CHAN_VOICE, ent->moveinfo.sound_end, 1, ATTN_STATIC, 0);
  364.         ent->s.sound = 0;
  365.     }
  366.     ent->moveinfo.state = STATE_TOP;
  367.  
  368.     ent->think = plat_go_down;
  369.     ent->nextthink = level.time + 3;
  370. }
  371.  
  372. void plat_hit_bottom (edict_t *ent)
  373. {
  374.     if (!(ent->flags & FL_TEAMSLAVE))
  375.     {
  376.         if (ent->moveinfo.sound_end)
  377.             gi.sound (ent, CHAN_NO_PHS_ADD+CHAN_VOICE, ent->moveinfo.sound_end, 1, ATTN_STATIC, 0);
  378.         ent->s.sound = 0;
  379.     }
  380.     ent->moveinfo.state = STATE_BOTTOM;
  381. }
  382.  
  383. void plat_go_down (edict_t *ent)
  384. {
  385.     if (!(ent->flags & FL_TEAMSLAVE))
  386.     {
  387.         if (ent->moveinfo.sound_start)
  388.             gi.sound (ent, CHAN_NO_PHS_ADD+CHAN_VOICE, ent->moveinfo.sound_start, 1, ATTN_STATIC, 0);
  389.         ent->s.sound = ent->moveinfo.sound_middle;
  390.     }
  391.     ent->moveinfo.state = STATE_DOWN;
  392.     Move_Calc (ent, ent->moveinfo.end_origin, plat_hit_bottom);
  393. }
  394.  
  395. void plat_go_up (edict_t *ent)
  396. {
  397.     if (!(ent->flags & FL_TEAMSLAVE))
  398.     {
  399.         if (ent->moveinfo.sound_start)
  400.             gi.sound (ent, CHAN_NO_PHS_ADD+CHAN_VOICE, ent->moveinfo.sound_start, 1, ATTN_STATIC, 0);
  401.         ent->s.sound = ent->moveinfo.sound_middle;
  402.     }
  403.     ent->moveinfo.state = STATE_UP;
  404.     Move_Calc (ent, ent->moveinfo.start_origin, plat_hit_top);
  405. }
  406.  
  407. void plat_blocked (edict_t *self, edict_t *other)
  408. {
  409.     if (!(other->svflags & SVF_MONSTER) && (!other->client) )
  410.     {
  411.         // give it a chance to go away on it's own terms (like gibs)
  412.         T_Damage (other, self, self, vec3_origin, other->s.origin, vec3_origin, 100000, 1, 0, MOD_CRUSH);
  413.         // if it's still there, nuke it
  414.         if (other)
  415.             BecomeExplosion1 (other);
  416.         return;
  417.     }
  418.  
  419.     T_Damage (other, self, self, vec3_origin, other->s.origin, vec3_origin, self->dmg, 1, 0, MOD_CRUSH);
  420.  
  421.     if (self->moveinfo.state == STATE_UP)
  422.         plat_go_down (self);
  423.     else if (self->moveinfo.state == STATE_DOWN)
  424.         plat_go_up (self);
  425. }
  426.  
  427.  
  428. void Use_Plat (edict_t *ent, edict_t *other, edict_t *activator)
  429.     if (ent->think)
  430.         return;        // already down
  431.     plat_go_down (ent);
  432. }
  433.  
  434.  
  435. void Touch_Plat_Center (edict_t *ent, edict_t *other, cplane_t *plane, csurface_t *surf)
  436. {
  437.     if (!other->client)
  438.         return;
  439.         
  440.     if (other->health <= 0)
  441.         return;
  442.  
  443.     ent = ent->enemy;    // now point at the plat, not the trigger
  444.     if (ent->moveinfo.state == STATE_BOTTOM)
  445.         plat_go_up (ent);
  446.     else if (ent->moveinfo.state == STATE_TOP)
  447.         ent->nextthink = level.time + 1;    // the player is still on the plat, so delay going down
  448. }
  449.  
  450. void plat_spawn_inside_trigger (edict_t *ent)
  451. {
  452.     edict_t    *trigger;
  453.     vec3_t    tmin, tmax;
  454.  
  455. //
  456. // middle trigger
  457. //    
  458.     trigger = G_Spawn();
  459.     trigger->touch = Touch_Plat_Center;
  460.     trigger->movetype = MOVETYPE_NONE;
  461.     trigger->solid = SOLID_TRIGGER;
  462.     trigger->enemy = ent;
  463.     
  464.     tmin[0] = ent->mins[0] + 25;
  465.     tmin[1] = ent->mins[1] + 25;
  466.     tmin[2] = ent->mins[2];
  467.  
  468.     tmax[0] = ent->maxs[0] - 25;
  469.     tmax[1] = ent->maxs[1] - 25;
  470.     tmax[2] = ent->maxs[2] + 8;
  471.  
  472.     tmin[2] = tmax[2] - (ent->pos1[2] - ent->pos2[2] + st.lip);
  473.  
  474.     if (ent->spawnflags & PLAT_LOW_TRIGGER)
  475.         tmax[2] = tmin[2] + 8;
  476.     
  477.     if (tmax[0] - tmin[0] <= 0)
  478.     {
  479.         tmin[0] = (ent->mins[0] + ent->maxs[0]) *0.5;
  480.         tmax[0] = tmin[0] + 1;
  481.     }
  482.     if (tmax[1] - tmin[1] <= 0)
  483.     {
  484.         tmin[1] = (ent->mins[1] + ent->maxs[1]) *0.5;
  485.         tmax[1] = tmin[1] + 1;
  486.     }
  487.     
  488.     VectorCopy (tmin, trigger->mins);
  489.     VectorCopy (tmax, trigger->maxs);
  490.  
  491.     gi.linkentity (trigger);
  492. }
  493.  
  494.  
  495. /*QUAKED func_plat (0 .5 .8) ? PLAT_LOW_TRIGGER
  496. speed    default 150
  497.  
  498. Plats are always drawn in the extended position, so they will light correctly.
  499.  
  500. If the plat is the target of another trigger or button, it will start out disabled in the extended position until it is trigger, when it will lower and become a normal plat.
  501.  
  502. "speed"    overrides default 200.
  503. "accel" overrides default 500
  504. "lip"    overrides default 8 pixel lip
  505.  
  506. If the "height" key is set, that will determine the amount the plat moves, instead of being implicitly determoveinfoned by the model's height.
  507.  
  508. Set "sounds" to one of the following:
  509. 1) base fast
  510. 2) chain slow
  511. */
  512. void SP_func_plat (edict_t *ent)
  513. {
  514.     VectorClear (ent->s.angles);
  515.     ent->solid = SOLID_BSP;
  516.     ent->movetype = MOVETYPE_PUSH;
  517.  
  518.     gi.setmodel (ent, ent->model);
  519.  
  520.     ent->blocked = plat_blocked;
  521.  
  522.     if (!ent->speed)
  523.         ent->speed = 20;
  524.     else
  525.         ent->speed *= 0.1;
  526.  
  527.     if (!ent->accel)
  528.         ent->accel = 5;
  529.     else
  530.         ent->accel *= 0.1;
  531.  
  532.     if (!ent->decel)
  533.         ent->decel = 5;
  534.     else
  535.         ent->decel *= 0.1;
  536.  
  537.     if (!ent->dmg)
  538.         ent->dmg = 2;
  539.  
  540.     if (!st.lip)
  541.         st.lip = 8;
  542.  
  543.     // pos1 is the top position, pos2 is the bottom
  544.     VectorCopy (ent->s.origin, ent->pos1);
  545.     VectorCopy (ent->s.origin, ent->pos2);
  546.     if (st.height)
  547.         ent->pos2[2] -= st.height;
  548.     else
  549.         ent->pos2[2] -= (ent->maxs[2] - ent->mins[2]) - st.lip;
  550.  
  551.     ent->use = Use_Plat;
  552.  
  553.     plat_spawn_inside_trigger (ent);    // the "start moving" trigger    
  554.  
  555.     if (ent->targetname)
  556.     {
  557.         ent->moveinfo.state = STATE_UP;
  558.     }
  559.     else
  560.     {
  561.         VectorCopy (ent->pos2, ent->s.origin);
  562.         gi.linkentity (ent);
  563.         ent->moveinfo.state = STATE_BOTTOM;
  564.     }
  565.  
  566.     ent->moveinfo.speed = ent->speed;
  567.     ent->moveinfo.accel = ent->accel;
  568.     ent->moveinfo.decel = ent->decel;
  569.     ent->moveinfo.wait = ent->wait;
  570.     VectorCopy (ent->pos1, ent->moveinfo.start_origin);
  571.     VectorCopy (ent->s.angles, ent->moveinfo.start_angles);
  572.     VectorCopy (ent->pos2, ent->moveinfo.end_origin);
  573.     VectorCopy (ent->s.angles, ent->moveinfo.end_angles);
  574.  
  575.     ent->moveinfo.sound_start = gi.soundindex ("plats/pt1_strt.wav");
  576.     ent->moveinfo.sound_middle = gi.soundindex ("plats/pt1_mid.wav");
  577.     ent->moveinfo.sound_end = gi.soundindex ("plats/pt1_end.wav");
  578. }
  579.  
  580. //====================================================================
  581.  
  582. /*QUAKED func_rotating (0 .5 .8) ? START_ON REVERSE X_AXIS Y_AXIS TOUCH_PAIN STOP ANIMATED ANIMATED_FAST
  583. You need to have an origin brush as part of this entity.  The center of that brush will be
  584. the point around which it is rotated. It will rotate around the Z axis by default.  You can
  585. check either the X_AXIS or Y_AXIS box to change that.
  586.  
  587. "speed" determines how fast it moves; default value is 100.
  588. "dmg"    damage to inflict when blocked (2 default)
  589.  
  590. REVERSE will cause the it to rotate in the opposite direction.
  591. STOP mean it will stop moving instead of pushing entities
  592. */
  593.  
  594. void rotating_blocked (edict_t *self, edict_t *other)
  595. {
  596.     T_Damage (other, self, self, vec3_origin, other->s.origin, vec3_origin, self->dmg, 1, 0, MOD_CRUSH);
  597. }
  598.  
  599. void rotating_touch (edict_t *self, edict_t *other, cplane_t *plane, csurface_t *surf)
  600. {
  601.     if (self->avelocity[0] || self->avelocity[1] || self->avelocity[2])
  602.         T_Damage (other, self, self, vec3_origin, other->s.origin, vec3_origin, self->dmg, 1, 0, MOD_CRUSH);
  603. }
  604.  
  605. void rotating_use (edict_t *self, edict_t *other, edict_t *activator)
  606. {
  607.     if (!VectorCompare (self->avelocity, vec3_origin))
  608.     {
  609.         self->s.sound = 0;
  610.         VectorClear (self->avelocity);
  611.         self->touch = NULL;
  612.     }
  613.     else
  614.     {
  615.         self->s.sound = self->moveinfo.sound_middle;
  616.         VectorScale (self->movedir, self->speed, self->avelocity);
  617.         if (self->spawnflags & 16)
  618.             self->touch = rotating_touch;
  619.     }
  620. }
  621.  
  622. void SP_func_rotating (edict_t *ent)
  623. {
  624.     ent->solid = SOLID_BSP;
  625.     if (ent->spawnflags & 32)
  626.         ent->movetype = MOVETYPE_STOP;
  627.     else
  628.         ent->movetype = MOVETYPE_PUSH;
  629.  
  630.     // set the axis of rotation
  631.     VectorClear(ent->movedir);
  632.     if (ent->spawnflags & 4)
  633.         ent->movedir[2] = 1.0;
  634.     else if (ent->spawnflags & 8)
  635.         ent->movedir[0] = 1.0;
  636.     else // Z_AXIS
  637.         ent->movedir[1] = 1.0;
  638.  
  639.     // check for reverse rotation
  640.     if (ent->spawnflags & 2)
  641.         VectorNegate (ent->movedir, ent->movedir);
  642.  
  643.     if (!ent->speed)
  644.         ent->speed = 100;
  645.     if (!ent->dmg)
  646.         ent->dmg = 2;
  647.  
  648. //    ent->moveinfo.sound_middle = "doors/hydro1.wav";
  649.  
  650.     ent->use = rotating_use;
  651.     if (ent->dmg)
  652.         ent->blocked = rotating_blocked;
  653.  
  654.     if (ent->spawnflags & 1)
  655.         ent->use (ent, NULL, NULL);
  656.  
  657.     if (ent->spawnflags & 64)
  658.         ent->s.effects |= EF_ANIM_ALL;
  659.     if (ent->spawnflags & 128)
  660.         ent->s.effects |= EF_ANIM_ALLFAST;
  661.  
  662.     gi.setmodel (ent, ent->model);
  663.     gi.linkentity (ent);
  664. }
  665.  
  666. /*
  667. ======================================================================
  668.  
  669. BUTTONS
  670.  
  671. ======================================================================
  672. */
  673.  
  674. /*QUAKED func_button (0 .5 .8) ?
  675. When a button is touched, it moves some distance in the direction of it's angle, triggers all of it's targets, waits some time, then returns to it's original position where it can be triggered again.
  676.  
  677. "angle"        determines the opening direction
  678. "target"    all entities with a matching targetname will be used
  679. "speed"        override the default 40 speed
  680. "wait"        override the default 1 second wait (-1 = never return)
  681. "lip"        override the default 4 pixel lip remaining at end of move
  682. "health"    if set, the button must be killed instead of touched
  683. "sounds"
  684. 1) silent
  685. 2) steam metal
  686. 3) wooden clunk
  687. 4) metallic click
  688. 5) in-out
  689. */
  690.  
  691. void button_done (edict_t *self)
  692. {
  693.     self->moveinfo.state = STATE_BOTTOM;
  694.     self->s.effects &= ~EF_ANIM23;
  695.     self->s.effects |= EF_ANIM01;
  696. }
  697.  
  698. void button_return (edict_t *self)
  699. {
  700.     self->moveinfo.state = STATE_DOWN;
  701.  
  702.     Move_Calc (self, self->moveinfo.start_origin, button_done);
  703.  
  704.     self->s.frame = 0;
  705.  
  706.     if (self->health)
  707.         self->takedamage = DAMAGE_YES;
  708. }
  709.  
  710. void button_wait (edict_t *self)
  711. {
  712.     self->moveinfo.state = STATE_TOP;
  713.     self->s.effects &= ~EF_ANIM01;
  714.     self->s.effects |= EF_ANIM23;
  715.  
  716.     G_UseTargets (self, self->activator);
  717.     self->s.frame = 1;
  718.     if (self->moveinfo.wait >= 0)
  719.     {
  720.         self->nextthink = level.time + self->moveinfo.wait;
  721.         self->think = button_return;
  722.     }
  723. }
  724.  
  725. void button_fire (edict_t *self)
  726. {
  727.     if (self->moveinfo.state == STATE_UP || self->moveinfo.state == STATE_TOP)
  728.         return;
  729.  
  730.     self->moveinfo.state = STATE_UP;
  731.     if (self->moveinfo.sound_start && !(self->flags & FL_TEAMSLAVE))
  732.         gi.sound (self, CHAN_NO_PHS_ADD+CHAN_VOICE, self->moveinfo.sound_start, 1, ATTN_STATIC, 0);
  733.     Move_Calc (self, self->moveinfo.end_origin, button_wait);
  734. }
  735.  
  736. void button_use (edict_t *self, edict_t *other, edict_t *activator)
  737. {
  738.     self->activator = activator;
  739.     button_fire (self);
  740. }
  741.  
  742. void button_touch (edict_t *self, edict_t *other, cplane_t *plane, csurface_t *surf)
  743. {
  744.     if (!other->client)
  745.         return;
  746.  
  747.     if (other->health <= 0)
  748.         return;
  749.  
  750.     self->activator = other;
  751.     button_fire (self);
  752. }
  753.  
  754. void button_killed (edict_t *self, edict_t *inflictor, edict_t *attacker, int damage, vec3_t point)
  755. {
  756.     self->activator = attacker;
  757.     self->health = self->max_health;
  758.     self->takedamage = DAMAGE_NO;
  759.     button_fire (self);
  760. }
  761.  
  762. void SP_func_button (edict_t *ent)
  763. {
  764.     vec3_t    abs_movedir;
  765.     float    dist;
  766.  
  767.     G_SetMovedir (ent->s.angles, ent->movedir);
  768.     ent->movetype = MOVETYPE_STOP;
  769.     ent->solid = SOLID_BSP;
  770.     gi.setmodel (ent, ent->model);
  771.  
  772.     if (ent->sounds != 1)
  773.         ent->moveinfo.sound_start = gi.soundindex ("switches/butn2.wav");
  774.     
  775.     if (!ent->speed)
  776.         ent->speed = 40;
  777.     if (!ent->accel)
  778.         ent->accel = ent->speed;
  779.     if (!ent->decel)
  780.         ent->decel = ent->speed;
  781.  
  782.     if (!ent->wait)
  783.         ent->wait = 3;
  784.     if (!st.lip)
  785.         st.lip = 4;
  786.  
  787.     VectorCopy (ent->s.origin, ent->pos1);
  788.     abs_movedir[0] = fabs(ent->movedir[0]);
  789.     abs_movedir[1] = fabs(ent->movedir[1]);
  790.     abs_movedir[2] = fabs(ent->movedir[2]);
  791.     dist = abs_movedir[0] * ent->size[0] + abs_movedir[1] * ent->size[1] + abs_movedir[2] * ent->size[2] - st.lip;
  792.     VectorMA (ent->pos1, dist, ent->movedir, ent->pos2);
  793.  
  794.     ent->use = button_use;
  795.     ent->s.effects |= EF_ANIM01;
  796.  
  797.     if (ent->health)
  798.     {
  799.         ent->max_health = ent->health;
  800.         ent->die = button_killed;
  801.         ent->takedamage = DAMAGE_YES;
  802.     }
  803.     else if (! ent->targetname)
  804.         ent->touch = button_touch;
  805.  
  806.     ent->moveinfo.state = STATE_BOTTOM;
  807.  
  808.     ent->moveinfo.speed = ent->speed;
  809.     ent->moveinfo.accel = ent->accel;
  810.     ent->moveinfo.decel = ent->decel;
  811.     ent->moveinfo.wait = ent->wait;
  812.     VectorCopy (ent->pos1, ent->moveinfo.start_origin);
  813.     VectorCopy (ent->s.angles, ent->moveinfo.start_angles);
  814.     VectorCopy (ent->pos2, ent->moveinfo.end_origin);
  815.     VectorCopy (ent->s.angles, ent->moveinfo.end_angles);
  816.  
  817.     gi.linkentity (ent);
  818. }
  819.  
  820. /*
  821. ======================================================================
  822.  
  823. DOORS
  824.  
  825.   spawn a trigger surrounding the entire team unless it is
  826.   allready targeted by another
  827.  
  828. ======================================================================
  829. */
  830.  
  831. /*QUAKED func_door (0 .5 .8) ? START_OPEN x CRUSHER NOMONSTER ANIMATED TOGGLE ANIMATED_FAST
  832. TOGGLE        wait in both the start and end states for a trigger event.
  833. START_OPEN    the door to moves to its destination when spawned, and operate in reverse.  It is used to temporarily or permanently close off an area when triggered (not useful for touch or takedamage doors).
  834. NOMONSTER    monsters will not trigger this door
  835.  
  836. "message"    is printed when the door is touched if it is a trigger door and it hasn't been fired yet
  837. "angle"        determines the opening direction
  838. "targetname" if set, no touch field will be spawned and a remote button or trigger field activates the door.
  839. "health"    if set, door must be shot open
  840. "speed"        movement speed (100 default)
  841. "wait"        wait before returning (3 default, -1 = never return)
  842. "lip"        lip remaining at end of move (8 default)
  843. "dmg"        damage to inflict when blocked (2 default)
  844. "sounds"
  845. 1)    silent
  846. 2)    light
  847. 3)    medium
  848. 4)    heavy
  849. */
  850.  
  851. void door_use_areaportals (edict_t *self, qboolean open)
  852. {
  853.     edict_t    *t = NULL;
  854.  
  855.     if (!self->target)
  856.         return;
  857.  
  858.     while ((t = G_Find (t, FOFS(targetname), self->target)))
  859.     {
  860.         if (Q_stricmp(t->classname, "func_areaportal") == 0)
  861.         {
  862.             gi.SetAreaPortalState (t->style, open);
  863.         }
  864.     }
  865. }
  866.  
  867. void door_go_down (edict_t *self);
  868.  
  869. void door_hit_top (edict_t *self)
  870. {
  871.     if (!(self->flags & FL_TEAMSLAVE))
  872.     {
  873.         if (self->moveinfo.sound_end)
  874.             gi.sound (self, CHAN_NO_PHS_ADD+CHAN_VOICE, self->moveinfo.sound_end, 1, ATTN_STATIC, 0);
  875.         self->s.sound = 0;
  876.     }
  877.     self->moveinfo.state = STATE_TOP;
  878.     if (self->spawnflags & DOOR_TOGGLE)
  879.         return;
  880.     if (self->moveinfo.wait >= 0)
  881.     {
  882.         self->think = door_go_down;
  883.         self->nextthink = level.time + self->moveinfo.wait;
  884.     }
  885. }
  886.  
  887. void door_hit_bottom (edict_t *self)
  888. {
  889.     if (!(self->flags & FL_TEAMSLAVE))
  890.     {
  891.         if (self->moveinfo.sound_end)
  892.             gi.sound (self, CHAN_NO_PHS_ADD+CHAN_VOICE, self->moveinfo.sound_end, 1, ATTN_STATIC, 0);
  893.         self->s.sound = 0;
  894.     }
  895.     self->moveinfo.state = STATE_BOTTOM;
  896.     door_use_areaportals (self, false);
  897. }
  898.  
  899. void door_go_down (edict_t *self)
  900. {
  901.     if (!(self->flags & FL_TEAMSLAVE))
  902.     {
  903.         if (self->moveinfo.sound_start)
  904.             gi.sound (self, CHAN_NO_PHS_ADD+CHAN_VOICE, self->moveinfo.sound_start, 1, ATTN_STATIC, 0);
  905.         self->s.sound = self->moveinfo.sound_middle;
  906.     }
  907.     if (self->max_health)
  908.     {
  909.         self->takedamage = DAMAGE_YES;
  910.         self->health = self->max_health;
  911.     }
  912.     
  913.     self->moveinfo.state = STATE_DOWN;
  914.     if (strcmp(self->classname, "func_door") == 0)
  915.         Move_Calc (self, self->moveinfo.start_origin, door_hit_bottom);
  916.     else if (strcmp(self->classname, "func_door_rotating") == 0)
  917.         AngleMove_Calc (self, door_hit_bottom);
  918. }
  919.  
  920. void door_go_up (edict_t *self, edict_t *activator)
  921. {
  922.     if (self->moveinfo.state == STATE_UP)
  923.         return;        // already going up
  924.  
  925.     if (self->moveinfo.state == STATE_TOP)
  926.     {    // reset top wait time
  927.         if (self->moveinfo.wait >= 0)
  928.             self->nextthink = level.time + self->moveinfo.wait;
  929.         return;
  930.     }
  931.     
  932.     if (!(self->flags & FL_TEAMSLAVE))
  933.     {
  934.         if (self->moveinfo.sound_start)
  935.             gi.sound (self, CHAN_NO_PHS_ADD+CHAN_VOICE, self->moveinfo.sound_start, 1, ATTN_STATIC, 0);
  936.         self->s.sound = self->moveinfo.sound_middle;
  937.     }
  938.     self->moveinfo.state = STATE_UP;
  939.     if (strcmp(self->classname, "func_door") == 0)
  940.         Move_Calc (self, self->moveinfo.end_origin, door_hit_top);
  941.     else if (strcmp(self->classname, "func_door_rotating") == 0)
  942.         AngleMove_Calc (self, door_hit_top);
  943.  
  944.     G_UseTargets (self, activator);
  945.     door_use_areaportals (self, true);
  946. }
  947.  
  948. void door_use (edict_t *self, edict_t *other, edict_t *activator)
  949. {
  950.     edict_t    *ent;
  951.  
  952.     if (self->flags & FL_TEAMSLAVE)
  953.         return;
  954.  
  955.     if (self->spawnflags & DOOR_TOGGLE)
  956.     {
  957.         if (self->moveinfo.state == STATE_UP || self->moveinfo.state == STATE_TOP)
  958.         {
  959.             // trigger all paired doors
  960.             for (ent = self ; ent ; ent = ent->teamchain)
  961.             {
  962.                 ent->message = NULL;
  963.                 ent->touch = NULL;
  964.                 door_go_down (ent);
  965.             }
  966.             return;
  967.         }
  968.     }
  969.     
  970.     // trigger all paired doors
  971.     for (ent = self ; ent ; ent = ent->teamchain)
  972.     {
  973.         ent->message = NULL;
  974.         ent->touch = NULL;
  975.         door_go_up (ent, activator);
  976.     }
  977. };
  978.  
  979. void Touch_DoorTrigger (edict_t *self, edict_t *other, cplane_t *plane, csurface_t *surf)
  980. {
  981.     if (other->health <= 0)
  982.         return;
  983.  
  984.     if (!(other->svflags & SVF_MONSTER) && (!other->client))
  985.         return;
  986.  
  987.     if ((self->owner->spawnflags & DOOR_NOMONSTER) && (other->svflags & SVF_MONSTER))
  988.         return;
  989.  
  990.     if (level.time < self->touch_debounce_time)
  991.         return;
  992.     self->touch_debounce_time = level.time + 1.0;
  993.  
  994.     door_use (self->owner, other, other);
  995. }
  996.  
  997. void Think_CalcMoveSpeed (edict_t *self)
  998. {
  999.     edict_t    *ent;
  1000.     float    min;
  1001.     float    time;
  1002.     float    newspeed;
  1003.     float    ratio;
  1004.     float    dist;
  1005.  
  1006.     if (self->flags & FL_TEAMSLAVE)
  1007.         return;        // only the team master does this
  1008.  
  1009.     // find the smallest distance any member of the team will be moving
  1010.     min = fabs(self->moveinfo.distance);
  1011.     for (ent = self->teamchain; ent; ent = ent->teamchain)
  1012.     {
  1013.         dist = fabs(ent->moveinfo.distance);
  1014.         if (dist < min)
  1015.             min = dist;
  1016.     }
  1017.  
  1018.     time = min / self->moveinfo.speed;
  1019.  
  1020.     // adjust speeds so they will all complete at the same time
  1021.     for (ent = self; ent; ent = ent->teamchain)
  1022.     {
  1023.         newspeed = fabs(ent->moveinfo.distance) / time;
  1024.         ratio = newspeed / ent->moveinfo.speed;
  1025.         if (ent->moveinfo.accel == ent->moveinfo.speed)
  1026.             ent->moveinfo.accel = newspeed;
  1027.         else
  1028.             ent->moveinfo.accel *= ratio;
  1029.         if (ent->moveinfo.decel == ent->moveinfo.speed)
  1030.             ent->moveinfo.decel = newspeed;
  1031.         else
  1032.             ent->moveinfo.decel *= ratio;
  1033.         ent->moveinfo.speed = newspeed;
  1034.     }
  1035. }
  1036.  
  1037. void Think_SpawnDoorTrigger (edict_t *ent)
  1038. {
  1039.     edict_t        *other;
  1040.     vec3_t        mins, maxs;
  1041.  
  1042.     if (ent->flags & FL_TEAMSLAVE)
  1043.         return;        // only the team leader spawns a trigger
  1044.  
  1045.     VectorCopy (ent->absmin, mins);
  1046.     VectorCopy (ent->absmax, maxs);
  1047.  
  1048.     for (other = ent->teamchain ; other ; other=other->teamchain)
  1049.     {
  1050.         AddPointToBounds (other->absmin, mins, maxs);
  1051.         AddPointToBounds (other->absmax, mins, maxs);
  1052.     }
  1053.  
  1054.     // expand 
  1055.     mins[0] -= 60;
  1056.     mins[1] -= 60;
  1057.     maxs[0] += 60;
  1058.     maxs[1] += 60;
  1059.  
  1060.     other = G_Spawn ();
  1061.     VectorCopy (mins, other->mins);
  1062.     VectorCopy (maxs, other->maxs);
  1063.     other->owner = ent;
  1064.     other->solid = SOLID_TRIGGER;
  1065.     other->movetype = MOVETYPE_NONE;
  1066.     other->touch = Touch_DoorTrigger;
  1067.     gi.linkentity (other);
  1068.  
  1069.     if (ent->spawnflags & DOOR_START_OPEN)
  1070.         door_use_areaportals (ent, true);
  1071.  
  1072.     Think_CalcMoveSpeed (ent);
  1073. }
  1074.  
  1075. void door_blocked  (edict_t *self, edict_t *other)
  1076. {
  1077.     edict_t    *ent;
  1078.  
  1079.     if (!(other->svflags & SVF_MONSTER) && (!other->client) )
  1080.     {
  1081.         // give it a chance to go away on it's own terms (like gibs)
  1082.         T_Damage (other, self, self, vec3_origin, other->s.origin, vec3_origin, 100000, 1, 0, MOD_CRUSH);
  1083.         // if it's still there, nuke it
  1084.         if (other)
  1085.             BecomeExplosion1 (other);
  1086.         return;
  1087.     }
  1088.  
  1089.     T_Damage (other, self, self, vec3_origin, other->s.origin, vec3_origin, self->dmg, 1, 0, MOD_CRUSH);
  1090.  
  1091.     if (self->spawnflags & DOOR_CRUSHER)
  1092.         return;
  1093.  
  1094.  
  1095. // if a door has a negative wait, it would never come back if blocked,
  1096. // so let it just squash the object to death real fast
  1097.     if (self->moveinfo.wait >= 0)
  1098.     {
  1099.         if (self->moveinfo.state == STATE_DOWN)
  1100.         {
  1101.             for (ent = self->teammaster ; ent ; ent = ent->teamchain)
  1102.                 door_go_up (ent, ent->activator);
  1103.         }
  1104.         else
  1105.         {
  1106.             for (ent = self->teammaster ; ent ; ent = ent->teamchain)
  1107.                 door_go_down (ent);
  1108.         }
  1109.     }
  1110. }
  1111.  
  1112. void door_killed (edict_t *self, edict_t *inflictor, edict_t *attacker, int damage, vec3_t point)
  1113. {
  1114.     edict_t    *ent;
  1115.  
  1116.     for (ent = self->teammaster ; ent ; ent = ent->teamchain)
  1117.     {
  1118.         ent->health = ent->max_health;
  1119.         ent->takedamage = DAMAGE_NO;
  1120.     }
  1121.     door_use (self->teammaster, attacker, attacker);
  1122. }
  1123.  
  1124. void door_touch (edict_t *self, edict_t *other, cplane_t *plane, csurface_t *surf)
  1125. {
  1126.     if (!other->client)
  1127.         return;
  1128.  
  1129.     if (level.time < self->touch_debounce_time)
  1130.         return;
  1131.     self->touch_debounce_time = level.time + 5.0;
  1132.  
  1133.     gi.centerprintf (other, "%s", self->message);
  1134.     gi.sound (other, CHAN_AUTO, gi.soundindex ("misc/talk1.wav"), 1, ATTN_NORM, 0);
  1135. }
  1136.  
  1137. void SP_func_door (edict_t *ent)
  1138. {
  1139.     vec3_t    abs_movedir;
  1140.  
  1141.     if (ent->sounds != 1)
  1142.     {
  1143.         ent->moveinfo.sound_start = gi.soundindex  ("doors/dr1_strt.wav");
  1144.         ent->moveinfo.sound_middle = gi.soundindex  ("doors/dr1_mid.wav");
  1145.         ent->moveinfo.sound_end = gi.soundindex  ("doors/dr1_end.wav");
  1146.     }
  1147.  
  1148.     G_SetMovedir (ent->s.angles, ent->movedir);
  1149.     ent->movetype = MOVETYPE_PUSH;
  1150.     ent->solid = SOLID_BSP;
  1151.     gi.setmodel (ent, ent->model);
  1152.  
  1153.     ent->blocked = door_blocked;
  1154.     ent->use = door_use;
  1155.     
  1156.     if (!ent->speed)
  1157.         ent->speed = 100;
  1158.     if (deathmatch->value)
  1159.         ent->speed *= 2;
  1160.  
  1161.     if (!ent->accel)
  1162.         ent->accel = ent->speed;
  1163.     if (!ent->decel)
  1164.         ent->decel = ent->speed;
  1165.  
  1166.     if (!ent->wait)
  1167.         ent->wait = 3;
  1168.     if (!st.lip)
  1169.         st.lip = 8;
  1170.     if (!ent->dmg)
  1171.         ent->dmg = 2;
  1172.  
  1173.     // calculate second position
  1174.     VectorCopy (ent->s.origin, ent->pos1);
  1175.     abs_movedir[0] = fabs(ent->movedir[0]);
  1176.     abs_movedir[1] = fabs(ent->movedir[1]);
  1177.     abs_movedir[2] = fabs(ent->movedir[2]);
  1178.     ent->moveinfo.distance = abs_movedir[0] * ent->size[0] + abs_movedir[1] * ent->size[1] + abs_movedir[2] * ent->size[2] - st.lip;
  1179.     VectorMA (ent->pos1, ent->moveinfo.distance, ent->movedir, ent->pos2);
  1180.  
  1181.     // if it starts open, switch the positions
  1182.     if (ent->spawnflags & DOOR_START_OPEN)
  1183.     {
  1184.         VectorCopy (ent->pos2, ent->s.origin);
  1185.         VectorCopy (ent->pos1, ent->pos2);
  1186.         VectorCopy (ent->s.origin, ent->pos1);
  1187.     }
  1188.  
  1189.     ent->moveinfo.state = STATE_BOTTOM;
  1190.  
  1191.     if (ent->health)
  1192.     {
  1193.         ent->takedamage = DAMAGE_YES;
  1194.         ent->die = door_killed;
  1195.         ent->max_health = ent->health;
  1196.     }
  1197.     else if (ent->targetname && ent->message)
  1198.     {
  1199.         gi.soundindex ("misc/talk.wav");
  1200.         ent->touch = door_touch;
  1201.     }
  1202.     
  1203.     ent->moveinfo.speed = ent->speed;
  1204.     ent->moveinfo.accel = ent->accel;
  1205.     ent->moveinfo.decel = ent->decel;
  1206.     ent->moveinfo.wait = ent->wait;
  1207.     VectorCopy (ent->pos1, ent->moveinfo.start_origin);
  1208.     VectorCopy (ent->s.angles, ent->moveinfo.start_angles);
  1209.     VectorCopy (ent->pos2, ent->moveinfo.end_origin);
  1210.     VectorCopy (ent->s.angles, ent->moveinfo.end_angles);
  1211.  
  1212.     if (ent->spawnflags & 16)
  1213.         ent->s.effects |= EF_ANIM_ALL;
  1214.     if (ent->spawnflags & 64)
  1215.         ent->s.effects |= EF_ANIM_ALLFAST;
  1216.  
  1217.     // to simplify logic elsewhere, make non-teamed doors into a team of one
  1218.     if (!ent->team)
  1219.         ent->teammaster = ent;
  1220.  
  1221.     gi.linkentity (ent);
  1222.  
  1223.     ent->nextthink = level.time + FRAMETIME;
  1224.     if (ent->health || ent->targetname)
  1225.         ent->think = Think_CalcMoveSpeed;
  1226.     else
  1227.         ent->think = Think_SpawnDoorTrigger;
  1228. }
  1229.  
  1230.  
  1231. /*QUAKED func_door_rotating (0 .5 .8) ? START_OPEN REVERSE CRUSHER NOMONSTER ANIMATED TOGGLE X_AXIS Y_AXIS
  1232. TOGGLE causes the door to wait in both the start and end states for a trigger event.
  1233.  
  1234. START_OPEN    the door to moves to its destination when spawned, and operate in reverse.  It is used to temporarily or permanently close off an area when triggered (not useful for touch or takedamage doors).
  1235. NOMONSTER    monsters will not trigger this door
  1236.  
  1237. You need to have an origin brush as part of this entity.  The center of that brush will be
  1238. the point around which it is rotated. It will rotate around the Z axis by default.  You can
  1239. check either the X_AXIS or Y_AXIS box to change that.
  1240.  
  1241. "distance" is how many degrees the door will be rotated.
  1242. "speed" determines how fast the door moves; default value is 100.
  1243.  
  1244. REVERSE will cause the door to rotate in the opposite direction.
  1245.  
  1246. "message"    is printed when the door is touched if it is a trigger door and it hasn't been fired yet
  1247. "angle"        determines the opening direction
  1248. "targetname" if set, no touch field will be spawned and a remote button or trigger field activates the door.
  1249. "health"    if set, door must be shot open
  1250. "speed"        movement speed (100 default)
  1251. "wait"        wait before returning (3 default, -1 = never return)
  1252. "dmg"        damage to inflict when blocked (2 default)
  1253. "sounds"
  1254. 1)    silent
  1255. 2)    light
  1256. 3)    medium
  1257. 4)    heavy
  1258. */
  1259.  
  1260. void SP_func_door_rotating (edict_t *ent)
  1261. {
  1262.     VectorClear (ent->s.angles);
  1263.  
  1264.     // set the axis of rotation
  1265.     VectorClear(ent->movedir);
  1266.     if (ent->spawnflags & DOOR_X_AXIS)
  1267.         ent->movedir[2] = 1.0;
  1268.     else if (ent->spawnflags & DOOR_Y_AXIS)
  1269.         ent->movedir[0] = 1.0;
  1270.     else // Z_AXIS
  1271.         ent->movedir[1] = 1.0;
  1272.  
  1273.     // check for reverse rotation
  1274.     if (ent->spawnflags & DOOR_REVERSE)
  1275.         VectorNegate (ent->movedir, ent->movedir);
  1276.  
  1277.     if (!st.distance)
  1278.     {
  1279.         gi.dprintf("%s at %s with no distance set\n", ent->classname, vtos(ent->s.origin));
  1280.         st.distance = 90;
  1281.     }
  1282.  
  1283.     VectorCopy (ent->s.angles, ent->pos1);
  1284.     VectorMA (ent->s.angles, st.distance, ent->movedir, ent->pos2);
  1285.     ent->moveinfo.distance = st.distance;
  1286.  
  1287.     ent->movetype = MOVETYPE_PUSH;
  1288.     ent->solid = SOLID_BSP;
  1289.     gi.setmodel (ent, ent->model);
  1290.  
  1291.     ent->blocked = door_blocked;
  1292.     ent->use = door_use;
  1293.  
  1294.     if (!ent->speed)
  1295.         ent->speed = 100;
  1296.     if (!ent->accel)
  1297.         ent->accel = ent->speed;
  1298.     if (!ent->decel)
  1299.         ent->decel = ent->speed;
  1300.  
  1301.     if (!ent->wait)
  1302.         ent->wait = 3;
  1303.     if (!ent->dmg)
  1304.         ent->dmg = 2;
  1305.  
  1306.     if (ent->sounds != 1)
  1307.     {
  1308.         ent->moveinfo.sound_start = gi.soundindex  ("doors/dr1_strt.wav");
  1309.         ent->moveinfo.sound_middle = gi.soundindex  ("doors/dr1_mid.wav");
  1310.         ent->moveinfo.sound_end = gi.soundindex  ("doors/dr1_end.wav");
  1311.     }
  1312.  
  1313.     // if it starts open, switch the positions
  1314.     if (ent->spawnflags & DOOR_START_OPEN)
  1315.     {
  1316.         VectorCopy (ent->pos2, ent->s.angles);
  1317.         VectorCopy (ent->pos1, ent->pos2);
  1318.         VectorCopy (ent->s.angles, ent->pos1);
  1319.         VectorNegate (ent->movedir, ent->movedir);
  1320.     }
  1321.  
  1322.     if (ent->health)
  1323.     {
  1324.         ent->takedamage = DAMAGE_YES;
  1325.         ent->die = door_killed;
  1326.         ent->max_health = ent->health;
  1327.     }
  1328.     
  1329.     if (ent->targetname && ent->message)
  1330.     {
  1331.         gi.soundindex ("misc/talk.wav");
  1332.         ent->touch = door_touch;
  1333.     }
  1334.  
  1335.     ent->moveinfo.state = STATE_BOTTOM;
  1336.     ent->moveinfo.speed = ent->speed;
  1337.     ent->moveinfo.accel = ent->accel;
  1338.     ent->moveinfo.decel = ent->decel;
  1339.     ent->moveinfo.wait = ent->wait;
  1340.     VectorCopy (ent->s.origin, ent->moveinfo.start_origin);
  1341.     VectorCopy (ent->pos1, ent->moveinfo.start_angles);
  1342.     VectorCopy (ent->s.origin, ent->moveinfo.end_origin);
  1343.     VectorCopy (ent->pos2, ent->moveinfo.end_angles);
  1344.  
  1345.     if (ent->spawnflags & 16)
  1346.         ent->s.effects |= EF_ANIM_ALL;
  1347.  
  1348.     // to simplify logic elsewhere, make non-teamed doors into a team of one
  1349.     if (!ent->team)
  1350.         ent->teammaster = ent;
  1351.  
  1352.     gi.linkentity (ent);
  1353.  
  1354.     ent->nextthink = level.time + FRAMETIME;
  1355.     if (ent->health || ent->targetname)
  1356.         ent->think = Think_CalcMoveSpeed;
  1357.     else
  1358.         ent->think = Think_SpawnDoorTrigger;
  1359. }
  1360.  
  1361.  
  1362. /*QUAKED func_water (0 .5 .8) ? START_OPEN
  1363. func_water is a moveable water brush.  It must be targeted to operate.  Use a non-water texture at your own risk.
  1364.  
  1365. START_OPEN causes the water to move to its destination when spawned and operate in reverse.
  1366.  
  1367. "angle"        determines the opening direction (up or down only)
  1368. "speed"        movement speed (25 default)
  1369. "wait"        wait before returning (-1 default, -1 = TOGGLE)
  1370. "lip"        lip remaining at end of move (0 default)
  1371. "sounds"    (yes, these need to be changed)
  1372. 0)    no sound
  1373. 1)    water
  1374. 2)    lava
  1375. */
  1376.  
  1377. void SP_func_water (edict_t *self)
  1378. {
  1379.     vec3_t    abs_movedir;
  1380.  
  1381.     G_SetMovedir (self->s.angles, self->movedir);
  1382.     self->movetype = MOVETYPE_PUSH;
  1383.     self->solid = SOLID_BSP;
  1384.     gi.setmodel (self, self->model);
  1385.  
  1386.     switch (self->sounds)
  1387.     {
  1388.         default:
  1389.             break;
  1390.  
  1391.         case 1: // water
  1392.             self->moveinfo.sound_start = gi.soundindex  ("world/mov_watr.wav");
  1393.             self->moveinfo.sound_end = gi.soundindex  ("world/stp_watr.wav");
  1394.             break;
  1395.  
  1396.         case 2: // lava
  1397.             self->moveinfo.sound_start = gi.soundindex  ("world/mov_watr.wav");
  1398.             self->moveinfo.sound_end = gi.soundindex  ("world/stp_watr.wav");
  1399.             break;
  1400.     }
  1401.  
  1402.     // calculate second position
  1403.     VectorCopy (self->s.origin, self->pos1);
  1404.     abs_movedir[0] = fabs(self->movedir[0]);
  1405.     abs_movedir[1] = fabs(self->movedir[1]);
  1406.     abs_movedir[2] = fabs(self->movedir[2]);
  1407.     self->moveinfo.distance = abs_movedir[0] * self->size[0] + abs_movedir[1] * self->size[1] + abs_movedir[2] * self->size[2] - st.lip;
  1408.     VectorMA (self->pos1, self->moveinfo.distance, self->movedir, self->pos2);
  1409.  
  1410.     // if it starts open, switch the positions
  1411.     if (self->spawnflags & DOOR_START_OPEN)
  1412.     {
  1413.         VectorCopy (self->pos2, self->s.origin);
  1414.         VectorCopy (self->pos1, self->pos2);
  1415.         VectorCopy (self->s.origin, self->pos1);
  1416.     }
  1417.  
  1418.     VectorCopy (self->pos1, self->moveinfo.start_origin);
  1419.     VectorCopy (self->s.angles, self->moveinfo.start_angles);
  1420.     VectorCopy (self->pos2, self->moveinfo.end_origin);
  1421.     VectorCopy (self->s.angles, self->moveinfo.end_angles);
  1422.  
  1423.     self->moveinfo.state = STATE_BOTTOM;
  1424.  
  1425.     if (!self->speed)
  1426.         self->speed = 25;
  1427.     self->moveinfo.accel = self->moveinfo.decel = self->moveinfo.speed = self->speed;
  1428.  
  1429.     if (!self->wait)
  1430.         self->wait = -1;
  1431.     self->moveinfo.wait = self->wait;
  1432.  
  1433.     self->use = door_use;
  1434.  
  1435.     if (self->wait == -1)
  1436.         self->spawnflags |= DOOR_TOGGLE;
  1437.  
  1438.     self->classname = "func_door";
  1439.  
  1440.     gi.linkentity (self);
  1441. }
  1442.  
  1443.  
  1444. #define TRAIN_START_ON        1
  1445. #define TRAIN_TOGGLE        2
  1446. #define TRAIN_BLOCK_STOPS    4
  1447.  
  1448. /*QUAKED func_train (0 .5 .8) ? START_ON TOGGLE BLOCK_STOPS
  1449. Trains are moving platforms that players can ride.
  1450. The targets origin specifies the min point of the train at each corner.
  1451. The train spawns at the first target it is pointing at.
  1452. If the train is the target of a button or trigger, it will not begin moving until activated.
  1453. speed    default 100
  1454. dmg        default    2
  1455. noise    looping sound to play when the train is in motion
  1456.  
  1457. */
  1458. void train_next (edict_t *self);
  1459.  
  1460. void train_blocked (edict_t *self, edict_t *other)
  1461. {
  1462.     if (!(other->svflags & SVF_MONSTER) && (!other->client) )
  1463.     {
  1464.         // give it a chance to go away on it's own terms (like gibs)
  1465.         T_Damage (other, self, self, vec3_origin, other->s.origin, vec3_origin, 100000, 1, 0, MOD_CRUSH);
  1466.         // if it's still there, nuke it
  1467.         if (other)
  1468.             BecomeExplosion1 (other);
  1469.         return;
  1470.     }
  1471.  
  1472.     if (level.time < self->touch_debounce_time)
  1473.         return;
  1474.  
  1475.     if (!self->dmg)
  1476.         return;
  1477.     self->touch_debounce_time = level.time + 0.5;
  1478.     T_Damage (other, self, self, vec3_origin, other->s.origin, vec3_origin, self->dmg, 1, 0, MOD_CRUSH);
  1479. }
  1480.  
  1481. void train_wait (edict_t *self)
  1482. {
  1483.     if (self->target_ent->pathtarget)
  1484.     {
  1485.         char    *savetarget;
  1486.         edict_t    *ent;
  1487.  
  1488.         ent = self->target_ent;
  1489.         savetarget = ent->target;
  1490.         ent->target = ent->pathtarget;
  1491.         G_UseTargets (ent, self->activator);
  1492.         ent->target = savetarget;
  1493.  
  1494.         // make sure we didn't get killed by a killtarget
  1495.         if (!self->inuse)
  1496.             return;
  1497.     }
  1498.  
  1499.     if (self->moveinfo.wait)
  1500.     {
  1501.         if (self->moveinfo.wait > 0)
  1502.         {
  1503.             self->nextthink = level.time + self->moveinfo.wait;
  1504.             self->think = train_next;
  1505.         }
  1506.         else if (self->spawnflags & TRAIN_TOGGLE)  // && wait < 0
  1507.         {
  1508.             train_next (self);
  1509.             self->spawnflags &= ~TRAIN_START_ON;
  1510.             VectorClear (self->velocity);
  1511.             self->nextthink = 0;
  1512.         }
  1513.  
  1514.         if (!(self->flags & FL_TEAMSLAVE))
  1515.         {
  1516.             if (self->moveinfo.sound_end)
  1517.                 gi.sound (self, CHAN_NO_PHS_ADD+CHAN_VOICE, self->moveinfo.sound_end, 1, ATTN_STATIC, 0);
  1518.             self->s.sound = 0;
  1519.         }
  1520.     }
  1521.     else
  1522.     {
  1523.         train_next (self);
  1524.     }
  1525.     
  1526. }
  1527.  
  1528. void train_next (edict_t *self)
  1529. {
  1530.     edict_t        *ent;
  1531.     vec3_t        dest;
  1532.     qboolean    first;
  1533.  
  1534.     first = true;
  1535. again:
  1536.     if (!self->target)
  1537.     {
  1538. //        gi.dprintf ("train_next: no next target\n");
  1539.         return;
  1540.     }
  1541.  
  1542.     ent = G_PickTarget (self->target);
  1543.     if (!ent)
  1544.     {
  1545.         gi.dprintf ("train_next: bad target %s\n", self->target);
  1546.         return;
  1547.     }
  1548.  
  1549.     self->target = ent->target;
  1550.  
  1551.     // check for a teleport path_corner
  1552.     if (ent->spawnflags & 1)
  1553.     {
  1554.         if (!first)
  1555.         {
  1556.             gi.dprintf ("connected teleport path_corners, see %s at %s\n", ent->classname, vtos(ent->s.origin));
  1557.             return;
  1558.         }
  1559.         first = false;
  1560.         VectorSubtract (ent->s.origin, self->mins, self->s.origin);
  1561.         VectorCopy (self->s.origin, self->s.old_origin);
  1562.         gi.linkentity (self);
  1563.         goto again;
  1564.     }
  1565.  
  1566.     self->moveinfo.wait = ent->wait;
  1567.     self->target_ent = ent;
  1568.  
  1569.     if (!(self->flags & FL_TEAMSLAVE))
  1570.     {
  1571.         if (self->moveinfo.sound_start)
  1572.             gi.sound (self, CHAN_NO_PHS_ADD+CHAN_VOICE, self->moveinfo.sound_start, 1, ATTN_STATIC, 0);
  1573.         self->s.sound = self->moveinfo.sound_middle;
  1574.     }
  1575.  
  1576.     VectorSubtract (ent->s.origin, self->mins, dest);
  1577.     self->moveinfo.state = STATE_TOP;
  1578.     VectorCopy (self->s.origin, self->moveinfo.start_origin);
  1579.     VectorCopy (dest, self->moveinfo.end_origin);
  1580.     Move_Calc (self, dest, train_wait);
  1581.     self->spawnflags |= TRAIN_START_ON;
  1582. }
  1583.  
  1584. void train_resume (edict_t *self)
  1585. {
  1586.     edict_t    *ent;
  1587.     vec3_t    dest;
  1588.  
  1589.     ent = self->target_ent;
  1590.  
  1591.     VectorSubtract (ent->s.origin, self->mins, dest);
  1592.     self->moveinfo.state = STATE_TOP;
  1593.     VectorCopy (self->s.origin, self->moveinfo.start_origin);
  1594.     VectorCopy (dest, self->moveinfo.end_origin);
  1595.     Move_Calc (self, dest, train_wait);
  1596.     self->spawnflags |= TRAIN_START_ON;
  1597. }
  1598.  
  1599. void func_train_find (edict_t *self)
  1600. {
  1601.     edict_t *ent;
  1602.  
  1603.     if (!self->target)
  1604.     {
  1605.         gi.dprintf ("train_find: no target\n");
  1606.         return;
  1607.     }
  1608.     ent = G_PickTarget (self->target);
  1609.     if (!ent)
  1610.     {
  1611.         gi.dprintf ("train_find: target %s not found\n", self->target);
  1612.         return;
  1613.     }
  1614.     self->target = ent->target;
  1615.  
  1616.     VectorSubtract (ent->s.origin, self->mins, self->s.origin);
  1617.     gi.linkentity (self);
  1618.  
  1619.     // if not triggered, start immediately
  1620.     if (!self->targetname)
  1621.         self->spawnflags |= TRAIN_START_ON;
  1622.  
  1623.     if (self->spawnflags & TRAIN_START_ON)
  1624.     {
  1625.         self->nextthink = level.time + FRAMETIME;
  1626.         self->think = train_next;
  1627.         self->activator = self;
  1628.     }
  1629. }
  1630.  
  1631. void train_use (edict_t *self, edict_t *other, edict_t *activator)
  1632. {
  1633.     self->activator = activator;
  1634.  
  1635.     if (self->spawnflags & TRAIN_START_ON)
  1636.     {
  1637.         if (!(self->spawnflags & TRAIN_TOGGLE))
  1638.             return;
  1639.         self->spawnflags &= ~TRAIN_START_ON;
  1640.         VectorClear (self->velocity);
  1641.         self->nextthink = 0;
  1642.     }
  1643.     else
  1644.     {
  1645.         if (self->target_ent)
  1646.             train_resume(self);
  1647.         else
  1648.             train_next(self);
  1649.     }
  1650. }
  1651.  
  1652. void SP_func_train (edict_t *self)
  1653. {
  1654.     self->movetype = MOVETYPE_PUSH;
  1655.  
  1656.     VectorClear (self->s.angles);
  1657.     self->blocked = train_blocked;
  1658.     if (self->spawnflags & TRAIN_BLOCK_STOPS)
  1659.         self->dmg = 0;
  1660.     else
  1661.     {
  1662.         if (!self->dmg)
  1663.             self->dmg = 100;
  1664.     }
  1665.     self->solid = SOLID_BSP;
  1666.     gi.setmodel (self, self->model);
  1667.  
  1668.     if (st.noise)
  1669.         self->moveinfo.sound_middle = gi.soundindex  (st.noise);
  1670.  
  1671.     if (!self->speed)
  1672.         self->speed = 100;
  1673.  
  1674.     self->moveinfo.speed = self->speed;
  1675.     self->moveinfo.accel = self->moveinfo.decel = self->moveinfo.speed;
  1676.  
  1677.     self->use = train_use;
  1678.  
  1679.     gi.linkentity (self);
  1680.  
  1681.     if (self->target)
  1682.     {
  1683.         // start trains on the second frame, to make sure their targets have had
  1684.         // a chance to spawn
  1685.         self->nextthink = level.time + FRAMETIME;
  1686.         self->think = func_train_find;
  1687.     }
  1688.     else
  1689.     {
  1690.         gi.dprintf ("func_train without a target at %s\n", vtos(self->absmin));
  1691.     }
  1692. }
  1693.  
  1694.  
  1695. /*QUAKED trigger_elevator (0.3 0.1 0.6) (-8 -8 -8) (8 8 8)
  1696. */
  1697. void trigger_elevator_use (edict_t *self, edict_t *other, edict_t *activator)
  1698. {
  1699.     edict_t *target;
  1700.  
  1701.     if (self->movetarget->nextthink)
  1702.     {
  1703. //        gi.dprintf("elevator busy\n");
  1704.         return;
  1705.     }
  1706.  
  1707.     if (!other->pathtarget)
  1708.     {
  1709.         gi.dprintf("elevator used with no pathtarget\n");
  1710.         return;
  1711.     }
  1712.  
  1713.     target = G_PickTarget (other->pathtarget);
  1714.     if (!target)
  1715.     {
  1716.         gi.dprintf("elevator used with bad pathtarget: %s\n", other->pathtarget);
  1717.         return;
  1718.     }
  1719.  
  1720.     self->movetarget->target_ent = target;
  1721.     train_resume (self->movetarget);
  1722. }
  1723.  
  1724. void trigger_elevator_init (edict_t *self)
  1725. {
  1726.     if (!self->target)
  1727.     {
  1728.         gi.dprintf("trigger_elevator has no target\n");
  1729.         return;
  1730.     }
  1731.     self->movetarget = G_PickTarget (self->target);
  1732.     if (!self->movetarget)
  1733.     {
  1734.         gi.dprintf("trigger_elevator unable to find target %s\n", self->target);
  1735.         return;
  1736.     }
  1737.     if (strcmp(self->movetarget->classname, "func_train") != 0)
  1738.     {
  1739.         gi.dprintf("trigger_elevator target %s is not a train\n", self->target);
  1740.         return;
  1741.     }
  1742.  
  1743.     self->use = trigger_elevator_use;
  1744.     self->svflags = SVF_NOCLIENT;
  1745.  
  1746. }
  1747.  
  1748. void SP_trigger_elevator (edict_t *self)
  1749. {
  1750.     self->think = trigger_elevator_init;
  1751.     self->nextthink = level.time + FRAMETIME;
  1752. }
  1753.  
  1754.  
  1755. /*QUAKED func_timer (0.3 0.1 0.6) (-8 -8 -8) (8 8 8) START_ON
  1756. "wait"            base time between triggering all targets, default is 1
  1757. "random"        wait variance, default is 0
  1758.  
  1759. so, the basic time between firing is a random time between
  1760. (wait - random) and (wait + random)
  1761.  
  1762. "delay"            delay before first firing when turned on, default is 0
  1763.  
  1764. "pausetime"        additional delay used only the very first time
  1765.                 and only if spawned with START_ON
  1766.  
  1767. These can used but not touched.
  1768. */
  1769. void func_timer_think (edict_t *self)
  1770. {
  1771.     G_UseTargets (self, self->activator);
  1772.     self->nextthink = level.time + self->wait + crandom() * self->random;
  1773. }
  1774.  
  1775. void func_timer_use (edict_t *self, edict_t *other, edict_t *activator)
  1776. {
  1777.     self->activator = activator;
  1778.  
  1779.     // if on, turn it off
  1780.     if (self->nextthink)
  1781.     {
  1782.         self->nextthink = 0;
  1783.         return;
  1784.     }
  1785.  
  1786.     // turn it on
  1787.     if (self->delay)
  1788.         self->nextthink = level.time + self->delay;
  1789.     else
  1790.         func_timer_think (self);
  1791. }
  1792.  
  1793. void SP_func_timer (edict_t *self)
  1794. {
  1795.     if (!self->wait)
  1796.         self->wait = 1.0;
  1797.  
  1798.     self->use = func_timer_use;
  1799.     self->think = func_timer_think;
  1800.  
  1801.     if (self->random >= self->wait)
  1802.     {
  1803.         self->random = self->wait - FRAMETIME;
  1804.         gi.dprintf("func_timer at %s has random >= wait\n", vtos(self->s.origin));
  1805.     }
  1806.  
  1807.     if (self->spawnflags & 1)
  1808.     {
  1809.         self->nextthink = level.time + 1.0 + st.pausetime + self->delay + self->wait + crandom() * self->random;
  1810.         self->activator = self;
  1811.     }
  1812.  
  1813.     self->svflags = SVF_NOCLIENT;
  1814. }
  1815.  
  1816.  
  1817. /*QUAKED func_conveyor (0 .5 .8) ? START_ON TOGGLE
  1818. Conveyors are stationary brushes that move what's on them.
  1819. The brush should be have a surface with at least one current content enabled.
  1820. speed    default 100
  1821. */
  1822.  
  1823. void func_conveyor_use (edict_t *self, edict_t *other, edict_t *activator)
  1824. {
  1825.     if (self->spawnflags & 1)
  1826.     {
  1827.         self->speed = 0;
  1828.         self->spawnflags &= ~1;
  1829.     }
  1830.     else
  1831.     {
  1832.         self->speed = self->count;
  1833.         self->spawnflags |= 1;
  1834.     }
  1835.  
  1836.     if (!(self->spawnflags & 2))
  1837.         self->count = 0;
  1838. }
  1839.  
  1840. void SP_func_conveyor (edict_t *self)
  1841. {
  1842.     if (!self->speed)
  1843.         self->speed = 100;
  1844.  
  1845.     if (!(self->spawnflags & 1))
  1846.     {
  1847.         self->count = self->speed;
  1848.         self->speed = 0;
  1849.     }
  1850.  
  1851.     self->use = func_conveyor_use;
  1852.  
  1853.     gi.setmodel (self, self->model);
  1854.     self->solid = SOLID_BSP;
  1855.     gi.linkentity (self);
  1856. }
  1857.  
  1858.  
  1859. /*QUAKED func_door_secret (0 .5 .8) ? always_shoot 1st_left 1st_down
  1860. A secret door.  Slide back and then to the side.
  1861.  
  1862. open_once        doors never closes
  1863. 1st_left        1st move is left of arrow
  1864. 1st_down        1st move is down from arrow
  1865. always_shoot    door is shootebale even if targeted
  1866.  
  1867. "angle"        determines the direction
  1868. "dmg"        damage to inflic when blocked (default 2)
  1869. "wait"        how long to hold in the open position (default 5, -1 means hold)
  1870. */
  1871.  
  1872. #define SECRET_ALWAYS_SHOOT    1
  1873. #define SECRET_1ST_LEFT        2
  1874. #define SECRET_1ST_DOWN        4
  1875.  
  1876. void door_secret_move1 (edict_t *self);
  1877. void door_secret_move2 (edict_t *self);
  1878. void door_secret_move3 (edict_t *self);
  1879. void door_secret_move4 (edict_t *self);
  1880. void door_secret_move5 (edict_t *self);
  1881. void door_secret_move6 (edict_t *self);
  1882. void door_secret_done (edict_t *self);
  1883.  
  1884. void door_secret_use (edict_t *self, edict_t *other, edict_t *activator)
  1885. {
  1886.     // make sure we're not already moving
  1887.     if (!VectorCompare(self->s.origin, vec3_origin))
  1888.         return;
  1889.  
  1890.     Move_Calc (self, self->pos1, door_secret_move1);
  1891.     door_use_areaportals (self, true);
  1892. }
  1893.  
  1894. void door_secret_move1 (edict_t *self)
  1895. {
  1896.     self->nextthink = level.time + 1.0;
  1897.     self->think = door_secret_move2;
  1898. }
  1899.  
  1900. void door_secret_move2 (edict_t *self)
  1901. {
  1902.     Move_Calc (self, self->pos2, door_secret_move3);
  1903. }
  1904.  
  1905. void door_secret_move3 (edict_t *self)
  1906. {
  1907.     if (self->wait == -1)
  1908.         return;
  1909.     self->nextthink = level.time + self->wait;
  1910.     self->think = door_secret_move4;
  1911. }
  1912.  
  1913. void door_secret_move4 (edict_t *self)
  1914. {
  1915.     Move_Calc (self, self->pos1, door_secret_move5);
  1916. }
  1917.  
  1918. void door_secret_move5 (edict_t *self)
  1919. {
  1920.     self->nextthink = level.time + 1.0;
  1921.     self->think = door_secret_move6;
  1922. }
  1923.  
  1924. void door_secret_move6 (edict_t *self)
  1925. {
  1926.     Move_Calc (self, vec3_origin, door_secret_done);
  1927. }
  1928.  
  1929. void door_secret_done (edict_t *self)
  1930. {
  1931.     if (!(self->targetname) || (self->spawnflags & SECRET_ALWAYS_SHOOT))
  1932.     {
  1933.         self->health = 0;
  1934.         self->takedamage = DAMAGE_YES;
  1935.     }
  1936.     door_use_areaportals (self, false);
  1937. }
  1938.  
  1939. void door_secret_blocked  (edict_t *self, edict_t *other)
  1940. {
  1941.     if (!(other->svflags & SVF_MONSTER) && (!other->client) )
  1942.     {
  1943.         // give it a chance to go away on it's own terms (like gibs)
  1944.         T_Damage (other, self, self, vec3_origin, other->s.origin, vec3_origin, 100000, 1, 0, MOD_CRUSH);
  1945.         // if it's still there, nuke it
  1946.         if (other)
  1947.             BecomeExplosion1 (other);
  1948.         return;
  1949.     }
  1950.  
  1951.     if (level.time < self->touch_debounce_time)
  1952.         return;
  1953.     self->touch_debounce_time = level.time + 0.5;
  1954.  
  1955.     T_Damage (other, self, self, vec3_origin, other->s.origin, vec3_origin, self->dmg, 1, 0, MOD_CRUSH);
  1956. }
  1957.  
  1958. void door_secret_die (edict_t *self, edict_t *inflictor, edict_t *attacker, int damage, vec3_t point)
  1959. {
  1960.     self->takedamage = DAMAGE_NO;
  1961.     door_secret_use (self, attacker, attacker);
  1962. }
  1963.  
  1964. void SP_func_door_secret (edict_t *ent)
  1965. {
  1966.     vec3_t    forward, right, up;
  1967.     float    side;
  1968.     float    width;
  1969.     float    length;
  1970.  
  1971.     ent->moveinfo.sound_start = gi.soundindex  ("doors/dr1_strt.wav");
  1972.     ent->moveinfo.sound_middle = gi.soundindex  ("doors/dr1_mid.wav");
  1973.     ent->moveinfo.sound_end = gi.soundindex  ("doors/dr1_end.wav");
  1974.  
  1975.     ent->movetype = MOVETYPE_PUSH;
  1976.     ent->solid = SOLID_BSP;
  1977.     gi.setmodel (ent, ent->model);
  1978.  
  1979.     ent->blocked = door_secret_blocked;
  1980.     ent->use = door_secret_use;
  1981.  
  1982.     if (!(ent->targetname) || (ent->spawnflags & SECRET_ALWAYS_SHOOT))
  1983.     {
  1984.         ent->health = 0;
  1985.         ent->takedamage = DAMAGE_YES;
  1986.         ent->die = door_secret_die;
  1987.     }
  1988.  
  1989.     if (!ent->dmg)
  1990.         ent->dmg = 2;
  1991.  
  1992.     if (!ent->wait)
  1993.         ent->wait = 5;
  1994.  
  1995.     ent->moveinfo.accel =
  1996.     ent->moveinfo.decel =
  1997.     ent->moveinfo.speed = 50;
  1998.  
  1999.     // calculate positions
  2000.     AngleVectors (ent->s.angles, forward, right, up);
  2001.     VectorClear (ent->s.angles);
  2002.     side = 1.0 - (ent->spawnflags & SECRET_1ST_LEFT);
  2003.     if (ent->spawnflags & SECRET_1ST_DOWN)
  2004.         width = fabs(DotProduct(up, ent->size));
  2005.     else
  2006.         width = fabs(DotProduct(right, ent->size));
  2007.     length = fabs(DotProduct(forward, ent->size));
  2008.     if (ent->spawnflags & SECRET_1ST_DOWN)
  2009.         VectorMA (ent->s.origin, -1 * width, up, ent->pos1);
  2010.     else
  2011.         VectorMA (ent->s.origin, side * width, right, ent->pos1);
  2012.     VectorMA (ent->pos1, length, forward, ent->pos2);
  2013.  
  2014.     if (ent->health)
  2015.     {
  2016.         ent->takedamage = DAMAGE_YES;
  2017.         ent->die = door_killed;
  2018.         ent->max_health = ent->health;
  2019.     }
  2020.     else if (ent->targetname && ent->message)
  2021.     {
  2022.         gi.soundindex ("misc/talk.wav");
  2023.         ent->touch = door_touch;
  2024.     }
  2025.     
  2026.     ent->classname = "func_door";
  2027.  
  2028.     gi.linkentity (ent);
  2029. }
  2030.  
  2031.  
  2032. /*QUAKED func_killbox (1 0 0) ?
  2033. Kills everything inside when fired, irrespective of protection.
  2034. */
  2035. void use_killbox (edict_t *self, edict_t *other, edict_t *activator)
  2036. {
  2037.     KillBox (self);
  2038. }
  2039.  
  2040. void SP_func_killbox (edict_t *ent)
  2041. {
  2042.     gi.setmodel (ent, ent->model);
  2043.     ent->use = use_killbox;
  2044.     ent->svflags = SVF_NOCLIENT;
  2045. }
  2046.  
  2047.